package com.cqcvc.controller;


import com.alibaba.fastjson.JSON;
import com.cqcvc.config.Result;
import com.cqcvc.entity.UserRole;
import com.cqcvc.service.UserRoleService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author hunter
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/userRole")
public class UserRoleController {
    @Autowired
    private UserRoleService userRoleService;

    //删除已有的角色，增加新角色
    @GetMapping("/save")
    //@RequiresPermissions("userRole:save")
    @ApiOperation(value = "设置当前用户的角色")
    public String saveUserRole(Integer uid, List<Integer> roleIds){
        System.out.println("当前用户ID:"+uid);
        System.out.println("用户新角色ID:"+uid);
        //删除用户原来的角色信息
        Map<String,Object> map=userRoleService.searchByUid(uid);
        List<UserRole> list=(List<UserRole>)map.get("list");
        System.out.println(list);
        for(UserRole ur:list){
            userRoleService.delUserRole(ur);
        }
        //给当前用户添加新角色
        for(Integer rid:roleIds){
            UserRole ur=new UserRole(uid,rid);
            userRoleService.saveUserRole(ur);
        }
        return JSON.toJSONString(new Result().setCode(200).setMessage("成功保存用户角色信息！"));
    }

    //查找当前用户已有的角色
    @GetMapping("/select")
    //@RequiresPermissions("userRole:select")
    @ApiOperation(value = "查询当前用户已有的角色")
    public String selectByUId(Integer uid){
        System.out.println("用户ID:"+uid);
        Map<String, Object> map=userRoleService.searchByUid(uid);
        return JSON.toJSONString(new Result().setCode(200).setData(map));
    }

}
